package realtime.app.dws;/*
 *@ClassName : Dws_Test_Paper_TestPaper
 *@Description : $ {description}
 *@Author : NingYu
 *@Date : 2023-03-24 19:{MINUTE}
 */

import com.alibaba.fastjson.JSONObject;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.AsyncDataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import realtime.bean.TestPaperExamBean;
import realtime.bean.TestPaperScoreRangBean;
import realtime.func.AsyncDIMFunction;
import realtime.util.ClickHouseUtil;
import realtime.util.DateFormatUtil;
import realtime.util.MyKafkaUtil;

import java.math.BigDecimal;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

public class Dws_Test_ScoreRang_TestPaper {
    public static void main(String[] args) throws Exception {

        //TODO 1.获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //TODO 2.读取Kafka DWD层 加购主题数据
        String topic = "dwd_test_exam";
        String groupId = "test_paper_0325";
        DataStreamSource<String> kafkaDS = env.addSource(MyKafkaUtil.getFlinkKafkaConsumer(topic, groupId));

        //TODO 3.将数据转换为JSON对象
        SingleOutputStreamOperator<JSONObject> jsonDS = kafkaDS.flatMap(new FlatMapFunction<String, JSONObject>() {
            @Override
            public void flatMap(String s, Collector<JSONObject> collector) throws Exception {
                if (s != null) {
                    try {
                        JSONObject jsonObject = JSONObject.parseObject(s);
                        collector.collect(jsonObject);
                    } catch (Exception e) {
                        System.out.println(s+" : 为空数据,无法转换为json格式");
                    }
                }
            }
        });

        //TODO 4.提取时间生成Watermark
        SingleOutputStreamOperator<JSONObject> jsonWithWMDS = jsonDS
                .assignTimestampsAndWatermarks(WatermarkStrategy.<JSONObject>forBoundedOutOfOrderness(Duration.ofSeconds(2))
                        .withTimestampAssigner(new SerializableTimestampAssigner<JSONObject>() {
                            @Override
                            public long extractTimestamp(JSONObject jsonObject, long l) {
                                return jsonObject.getLong("create_time");
                            }
                        }));

        //TODO 5.按照 paper_id进行分组

        KeyedStream<JSONObject, String> keyedByPaperIdDS = jsonWithWMDS.keyBy(json -> json.getString("paper_id"));

        //TODO 6.去重并转换为JavaBean对象
        SingleOutputStreamOperator<TestPaperScoreRangBean> beanDS = keyedByPaperIdDS.flatMap(new RichFlatMapFunction<JSONObject, TestPaperScoreRangBean>() {

            private ValueState<String> testIdState;

            @Override
            public void open(Configuration parameters) throws Exception {

                testIdState = getRuntimeContext().getState(new ValueStateDescriptor<String>("paperId-state", String.class));
            }

            @Override
            public void flatMap(JSONObject jsonObject, Collector<TestPaperScoreRangBean> collector) throws Exception {

                String lastTestId = testIdState.value();//状态中paperId

                String curTestId = jsonObject.getString("id");// 当前courseId

                Double score = jsonObject.getDouble("score");
                if (score < 60){
                    collector.collect(TestPaperScoreRangBean.builder()
                            .personCt(1l)
                            .paperCt(1L)
                            .ts(jsonObject.getLong("submit_time"))
                            .paperId(jsonObject.getString("paper_id"))
                            .scoreRang("不及格")
                            .build());
                }else if (score <= 80){
                    collector.collect(TestPaperScoreRangBean.builder()
                            .personCt(1l)
                            .paperCt(1L)
                            .ts(jsonObject.getLong("submit_time"))
                            .paperId(jsonObject.getString("paper_id"))
                            .scoreRang("尚可")
                            .build());
                }else{
                    collector.collect(TestPaperScoreRangBean.builder()
                            .personCt(1l)
                            .paperCt(1L)
                            .ts(jsonObject.getLong("submit_time"))
                            .paperId(jsonObject.getString("paper_id"))
                            .scoreRang("优秀")
                            .build());
                }

            }
        });

        //todo 关联需要聚合的维度
        SingleOutputStreamOperator<TestPaperScoreRangBean> beanWithPaperNameDS = AsyncDataStream.unorderedWait(
                beanDS
                , new AsyncDIMFunction<TestPaperScoreRangBean>("dim_test_paper".toUpperCase()) {
                    @Override
                    public void join(TestPaperScoreRangBean input, JSONObject dimInfo) {
                        input.setPaperName(dimInfo.getString("paper_title".toUpperCase()));
                    }

                    @Override
                    public String getKey(TestPaperScoreRangBean input) {
                        return input.getPaperId();
                    }
                }
                , 60
                , TimeUnit.SECONDS
        );

        //TODO 7.开窗、聚合
        SingleOutputStreamOperator<TestPaperScoreRangBean> reduceDS = beanWithPaperNameDS
                .keyBy(new KeySelector<TestPaperScoreRangBean, Tuple2<String,String>>() {
                    @Override
                    public Tuple2<String, String> getKey(TestPaperScoreRangBean bean) throws Exception {
                        String paperName = bean.getPaperName();
                        String scoreRang = bean.getScoreRang();
                        return Tuple2.of(paperName,scoreRang);
                    }
                })
                .window(TumblingEventTimeWindows.of(Time.seconds(10)))
                .reduce(new ReduceFunction<TestPaperScoreRangBean>() {

                    @Override
                    public TestPaperScoreRangBean reduce(TestPaperScoreRangBean v1, TestPaperScoreRangBean v2) throws Exception {
                        v1.setPersonCt(v1.getPersonCt() + v2.getPersonCt());
                        v1.setPaperCt(v1.getPersonCt()+v2.getPaperCt());
                        return v1;
                    }
                }, new WindowFunction<TestPaperScoreRangBean, TestPaperScoreRangBean, Tuple2<String, String>, TimeWindow>() {
                    @Override
                    public void apply(Tuple2<String, String> stringStringTuple2, TimeWindow timeWindow, Iterable<TestPaperScoreRangBean> iterable, Collector<TestPaperScoreRangBean> collector) throws Exception {

                        TestPaperScoreRangBean next = iterable.iterator().next();

                        next.setStt(DateFormatUtil.toYmdHms(timeWindow.getStart()));
                        next.setEdt(DateFormatUtil.toYmdHms(timeWindow.getEnd()));
                        next.setTs(System.currentTimeMillis());

                        collector.collect(next);
                    }
                });


        //TODO 8.将数据写出
        reduceDS.print("Dws_Test_ScoreRang_TestPaper>>>>>>>>\n");

        reduceDS.addSink(ClickHouseUtil.getSinkFunction("insert into dws_test_scoreRang_testpaper_window values(?,?,?,?,?,?,?,?)"));

        //TODO 9.启动任务
        env.execute();

    }


}
